﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TranTekBE.Kho
{
    public partial class CuaHangNhan : PagePopUp
    {
        public DataRow row = null;
        public string ChuyenHangId = "";
        public Database database = new Database(TinyLib.GetConfigKey("ConnectionString2"));
        protected void Page_Load(object sender, EventArgs e)
        {
            ChuyenHangId = Request.QueryString["ChuyenHangId"];
            row = GetRow();
            if (!IsPostBack)
            {
                lblCuaHangChuyen.Text = row["CHName"].ToString() + " (Ngày: " + DateTime.Parse(row["Ngay"].ToString()).ToString("dd/MM/yyyy") + ")";
                lblMaChuyenHang.Text = ChuyenHangId.ToString();
                var dset = database.ExecuteQuery(@"SELECT [C007001] CHName FROM [192.168.1.98\CAMSOFT].[POSPRO_20130107].[dbo].[T0070] where [C007001] not in ('RACHMIEU','PMT')");
                ddlCuaHang.Items.Clear();
                for (int i = 0; i < dset.Tables[0].Rows.Count; i++)
                {
                    ddlCuaHang.Items.Add(new ListItem()
                    {
                        Text = dset.Tables[0].Rows[i]["CHName"].ToString(),
                        Value = dset.Tables[0].Rows[i]["CHName"].ToString(),
                        Selected = dset.Tables[0].Rows[i]["CHName"].ToString() == row["CuaHangNhan"].ToString(),
                        Enabled = dset.Tables[0].Rows[i]["CHName"].ToString() != row["CHName"].ToString()
                    });

                }

                //if (string.IsNullOrEmpty(row["CuaHangNhan"].ToString()))
                //{
                //    //new
                //    ThuVien.FunctionsUtils.BindDataForControlList(ddlCuaHang, ds, "CHName", "CHName", "", "--Tất cả--");

                //}
                //else { 
                ////update
                //}



            }


        }
        DataRow GetRow()
        {
            DataRow rowwww = null;
             string sql = @"SELECT l.*,r.CuaHangChuyen,r.CuaHangNhan,r.UserChuyen,r.UserNhan,r.UserXuLy,r.DaChuyen,r.DaNhan,r.KhoXuLy,r.NgayChuyen,r.NgayNhan,r.CreatedDate FROM (
			SELECT C002301 ChuyenHangId,C002307 Ngay,C002319 CHName  FROM [192.168.1.98\CAMSOFT].[POSPRO_20130107].[dbo].[T0023] 
			WHERE C002301 LIKE '%TH-%' 
			AND C002304 < 0 
			AND C002301='{0}'
			) AS l 
			LEFT JOIN [Pharmacity_Project].[dbo].[ChuyenHang] AS r 
			ON l.ChuyenHangId=r.ChuyenHangId";

                var ds = database.ExecuteQuery(string.Format(sql, ChuyenHangId));
                if (ds.Tables.Count > 0)
                {
                    var dt = ds.Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        rowwww = dt.Rows[0];
                    }
                }
                return rowwww;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(row["CuaHangNhan"].ToString()))
            {
               //insert
                string str = @"INSERT INTO [Pharmacity_Project].[dbo].[ChuyenHang]
                           ([ChuyenHangId]
                           ,[CuaHangChuyen]
                           ,[UserChuyen]
                           ,[CuaHangNhan]
                           ,[UserNhan]
                           ,[DaChuyen]
                           ,[DaNhan]
                           ,[KhoXuLy]
                           ,[UserXuLy]
                           ,[NgayChuyen]
                           ,[NgayNhan]
                           ,[CreatedDate])
                     VALUES
                           ('{0}'
                           ,'{1}'
                           ,'{3}'
                           ,'{2}'
                           ,null
                           ,1
                           ,0
                           ,0
                           ,null
                           ,'{4}'
                           ,null
                           ,GETDATE())";
                string sql = string.Format(str,
                    ChuyenHangId,//0
                    row["CHName"].ToString(),//1
                    ddlCuaHang.SelectedValue,//2
                    UserInfo.UserID,//3
                    DateTime.Now//4
                    );

                database.ExecuteQuery(sql);
                string a = "[{3}][{0}] thêm cửa hàng nhận là {1} lúc {2}";
                GhiLog("Thêm cửa hàng nhận", string.Format(a, UserInfo.UserName, ddlCuaHang.SelectedValue, DateTime.Now,UserInfo.Stores));
            }
            else
            {
                //update

                string s = "UPDATE [Pharmacity_Project].[dbo].[ChuyenHang] SET [CuaHangNhan] = '{0}', NgayChuyen='{2}'  WHERE [ChuyenHangId]='{1}'";
                database.ExecuteQuery(string.Format(s,ddlCuaHang.SelectedValue,ChuyenHangId,DateTime.Now));
                string a = "[{4}][{0}] sửa cửa hàng nhận {1} thành {2} lúc {3}";
                GhiLog("Sửa cửa hàng nhận", string.Format(a, UserInfo.UserName, row["CuaHangNhan"], ddlCuaHang.SelectedValue, DateTime.Now, UserInfo.Stores));

            }
            Response.Write("<script>parent.CloseDialog();parent.ReloadForm();</script>");


        }
        void GhiLog(string action="",string detail="")
        {
            string s = @"INSERT INTO [Pharmacity_Project].[dbo].[Log_ChuyenHang]
           ([ChuyenHangId]
           ,[UserId]
           ,[HanhDong]
           ,[Detail]
           ,[CreatedDate])
     VALUES
           ('{0}'
           ,'{1}'
           ,N'{2}'
           ,N'{3}'
           ,GETDATE())";

            string sql = string.Format(s, ChuyenHangId, UserInfo.UserID, action, detail);
            database.ExecuteQuery(sql);
        }
    }
}